const puppeteer = require('puppeteer');
const xlsx = require('node-xlsx');
const GoLogin = require('../src/gologin.js');
const setting = require('./setting');

(async () => {
    try {
        let num = 23;
        let proxy = setting.proxy;
        const sheets = xlsx.parse(setting.excelPath);
        let sheetElement = sheets[0].data[num];
        console.log("sheets data：", sheetElement)
        const birthday = sheetElement[2];
        let date = new Date(1900, 0, birthday - 1);
        console.log(date.toLocaleDateString())
        const mail = sheetElement[4];
        const profileId = sheetElement[7];
        const developerName = sheetElement[8];
        const developerPhone = sheetElement[9];
        setting.getProxy()
        const GL = new GoLogin({
            token: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiI2MjhjYTJkNDQ2NDc4ZDZkMzE2Njc0NWUiLCJ0eXBlIjoiZGV2Iiwiand0aWQiOiI2MjhjYTNkYWY3ZjExMTcxMTZjZGRlNDYifQ.dGK_cQSjS1DO1AvK433tpviT6AkpYfd2tjnU4zVKERI',
            profile_id: profileId,
            extra_params: ['--start-maximized', '--proxy-server=socks5://' + proxy],
            uploadCookiesToServer: true,
            writeCookesFromServer: true
        });


        const {status, wsUrl} = await GL.start();

        const browser = await puppeteer.connect({
            browserWSEndpoint: wsUrl.toString(),
            ignoreHTTPSErrors: true,
        });
        const page = await browser.newPage();
        page.setDefaultNavigationTimeout(0);
        page.setDefaultTimeout(0);
        await page.setViewport({width: 1920, height: 1080});
        console.log('打开开发者注册网站')
        await page.goto('https://play.google.com/console/signup');

        try {
            let promise = await page.$$('input');
            await promise[0].type(developerName,{delay:50})
            await promise[1].type(mail+'@gmail.com',{delay:50})
            await promise[2].type(developerPhone,{delay:50})
            await promise[3].click({delay:500});
            await promise[4].click({delay:500});
            await autoScroll(page);
            await clickElm(page, '.button-content')
        } catch (e) {
            console.log(e);
        }
        await sleep(20000 + Math.random() * 10000)
        await GL.uploadProfileCookiesToServer();
    } catch (e) {
        console.log(e);
    }
})();

async function autoScroll(page) {
    await page.evaluate(async () => {
        await new Promise((resolve, reject) => {
            let totalHeight = 0;
            let distance = 100;
            let timer = setInterval(() => {
                let scrollHeight = document.body.scrollHeight;
                window.scrollBy(0, distance);
                totalHeight += distance;

                if (totalHeight >= scrollHeight) {

                    clearInterval(timer);
                    resolve();
                }
            }, 120);
        });
    });
}


async function clickElm(page, str) {
    await page.waitForSelector(str);
    let crtElement = await page.$(str);
    let crtBox = await crtElement.boundingBox();
    if (crtBox) {
        let xBox = crtBox.x + (crtBox.width * 2 / 3) * Math.random();
        let yBox = crtBox.y + (crtBox.height * 2 / 3) * Math.random();
        await page.mouse.move(xBox, yBox);
        await page.mouse.click(xBox, yBox, {delay: 800 + Math.random() * 2000});
    }
}

function sleep(ms) {
    return new Promise(resolve => {
        setTimeout(resolve, ms)
    })
}



